import webapp2, os, jinja2, logging, lxml.html
import datastore, util

DEVELOPMENT = os.environ['SERVER_SOFTWARE'].startswith('Development')

class GenericHandler(webapp2.RequestHandler):
    service_name = 'Generic'    

    def get(self):
        headers = self.request.headers                
        if DEVELOPMENT or (headers.has_key('X-Appengine-Cron') and headers['X-Appengine-Cron'] == 'true'):        
            newEntries = self.getNewEntries()
            logging.info("newEntries: %d" % len(newEntries))            
            isNew, content = self.compareWithNewEntries(newEntries)
            if isNew:
                util.sendMail(content)
        else:        
            content = 'This is not a cron request'
        self.writeResponse(content)
    
    def getNewEntries(self):
        # GET web site html
        try:        
            html = util.httpget('http://www.zeit.de')
            tree = lxml.html.fromstring(html)
            r = [lxml.html.tostring(entry) for entry in tree.cssselect('ul li.button')]
            logging.info(len(r))
            return r 
        except urllib2.URLError as e:
            logging.error(e)
        finally:
            return []

    def compareWithNewEntries(self, newEntries):
        # check number of entries in db
        dbEntries = datastore.getAll(self.service_name)
        dbEntriesSize = dbEntries.count()
        if dbEntriesSize == 0 and len(newEntries) > 0:
            # initialize Database
            for entry in newEntries:
                datastore.put(service_name, entry[0:400])
            return False, 'initialization with %d entries' % len(newEntries)
      
        if len(newEntries) > dbEntriesSize:
            dif = len(newEntries) - dbEntriesSize
            content = '<h3>new entries detected: </h3> \n';
            for i in range(dif):         
                datastore.put(service_name, newEntries[i][0:400])
                content += newEntries[i]
            return True, content
        else:
            return False, """no new entries or something went wrong. </br> 
                      service_name: %s </br>
                      dbEntries: %s </br> 
                      newEntries: %s
                   """ % (self.service_name, dbEntriesSize, len(newEntries))    

    def writeResponse(self, content):
        jinja_environment = jinja2.Environment(autoescape=False, 
            loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), '../templates')))    
        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render({'content': content}))

